package org.example.Soution002;

import java.util.*;
import java.util.stream.Collectors;

public class SumFromArray {
    public static void main(String[] args) {
        int[] nums={2};
        SumFromArray obj=new SumFromArray();
        System.out.println(obj.combinationSum(nums,1));
    }

    List<List<Integer>> result=new ArrayList<List<Integer>>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<Integer> newList= Arrays.stream(candidates)
                .boxed()
                .collect(Collectors.toList());
        for(int i=0;i<candidates.length;i++){
            List<Integer> list=new ArrayList<>();
            list.add(candidates[i]);
            sumFromCombine(newList,target,list,candidates[i]);
            newList.remove(0);
        }
        return result;
    }

    public void sumFromCombine(List<Integer> arrayList,int target,List<Integer> list,int listSum){
        if(listSum>target) return;
        if(listSum==target) {
            result.add(list);
        }
        List<Integer> tempArrayList=new ArrayList<>(arrayList);
        while(tempArrayList.size()>0){
            List<Integer> tempList=new ArrayList<>(list);
            tempList.add(tempArrayList.get(0));
            int tempSum=listSum+tempArrayList.get(0);
            sumFromCombine(tempArrayList,target,tempList,tempSum);
            tempArrayList.remove(0);
        }
    }

}
